from sys import argv
from os import system
#
print "sorting 1st time",
system('sort -T ./ ' + argv[1] + ' -o ' + argv[1]+'.sorted')
print "\tdone!"
#

##
print "merging",
f = open(argv[1]+'.sorted')
g = open(argv[1]+'.merged', 'w')
last = [1, f.readline()]
for line in f:
    if line != last[1]:
        g.write(str(last[0])+'\t'+last[1])
        last = [1, line]
    else:
        last[0] += 1
g.write(str(last[0])+'\t'+last[1])
f.close()
g.close()
system('rm ' + argv[1]+'.sorted')
print "\t\tdone!"
##

###
print "sorting 2nd time",
system("sort -T ./ -t'\t' -k 1 -g -r " + argv[1]+'.merged' + ' -o ' + argv[1]+'.sorted2')
system('rm ' + argv[1]+'.merged')
print "\tdone!"
###

####
print "export wordlist",
system("awk '{print $2}' " + argv[1]+'.sorted2 > ' + argv[1]+'.wordlist')
system('rm ' + argv[1]+'.sorted2')
print "\tdone!"
####
